package com.example.filter;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;

import java.io.IOException;

//@WebFilter(urlPatterns = "/*")
public class DemoFilter implements Filter {
    @Override //初始化方法, 只调用一次
    public void init(FilterConfig filterConfig) throws ServletException {
//        Filter.super.init(filterConfig);
        System.out.println("DemoFilter init");
    }

    @Override //拦截到请求之后调用, 调用多次
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //放行前逻辑
        System.out.println("DemoFilter doFilter");
        System.out.println("执行放行前逻辑");
        //放行==>访问对应的资源
        filterChain.doFilter(servletRequest, servletResponse);
        //放行后逻辑
        System.out.println("执行放行后逻辑");
    }

    @Override //销毁方法, 只调用一次
    public void destroy() {
//        Filter.super.destroy();
        System.out.println("DemoFilter destroy");
    }
}
